import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';

///商品评价
class GoodsEvaluate extends StatefulWidget {
  @override
  _GoodsEvaluateState createState() => _GoodsEvaluateState();
}

class _GoodsEvaluateState extends State<GoodsEvaluate> {
  List<double> likeCount = [0, 0];

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        _showDialog();
        return false;
      },
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          //输入框失焦
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Scaffold(
          backgroundColor: YTStyle.bgColor,
          appBar: AppBar(
            elevation: 0,
            centerTitle: true,
            backgroundColor: Colors.white,
            title: Text(
              "发表评价",
              style: TextStyle(
                  color: Color(0xff323232),
                  fontSize: setSp(18),
                  fontWeight: FontWeight.w600),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
            leading: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                _showDialog();
              },
              child: Row(
                children: [
                  Container(
                      margin: EdgeInsets.only(left: setWidth(15)),
                      width: setWidth(10),
                      height: setWidth(17),
                      child: Image.asset(
                        "assets/images/return.png",
                        fit: BoxFit.cover,
                      )),
                ],
              ),
            ),
          ),
          body: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //评价打分
                Container(
                    margin: EdgeInsets.only(top: setWidth(13)),
                    padding: EdgeInsets.only(
                        left: setWidth(25),
                        top: setWidth(15),
                        right: setWidth(25),
                        bottom: setWidth(15)),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(6)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "永达汽贸 [杭州]",
                              style: TextStyle(
                                  color: Color(0xff686868),
                                  fontSize: setSp(14)),
                            ),
                            SizedBox(
                              height: setWidth(28),
                            ),
                            Row(
                              children: [
                                Text(
                                  "商品评价",
                                  style: TextStyle(
                                      color: Color(0xff666666),
                                      fontSize: setSp(14),
                                      fontWeight: FontWeight.w600),
                                ),
                                SizedBox(
                                  width: setWidth(8),
                                ),
                                RatingBar(
                                  initialRating: likeCount[0],
                                  direction: Axis.horizontal,
                                  allowHalfRating: false,
                                  itemCount: 5,
                                  itemSize: 20,
                                  ratingWidget: RatingWidget(
                                    //自定义评分组件
                                    full: Image.asset(
                                        'assets/images/like_evaluate.png'),
                                    empty: Image.asset(
                                        'assets/images/unlike_evaluate.png'),
                                  ),
                                  itemPadding:
                                      EdgeInsets.symmetric(horizontal: 4.0),
                                  onRatingUpdate: (rating) {
                                    setState(() {
                                      this.likeCount[0] = rating;
                                    });
                                  },
                                ),
                              ],
                            ),
                            SizedBox(
                              height: setWidth(23),
                            ),
                            Row(
                              children: [
                                Text(
                                  "服务评价",
                                  style: TextStyle(
                                      color: Color(0xff666666),
                                      fontSize: setSp(14),
                                      fontWeight: FontWeight.w600),
                                ),
                                SizedBox(
                                  width: setWidth(8),
                                ),
                                RatingBar(
                                  initialRating: likeCount[1],
                                  direction: Axis.horizontal,
                                  allowHalfRating: false,
                                  itemCount: 5,
                                  itemSize: 20,
                                  ratingWidget: RatingWidget(
                                    //自定义评分组件
                                    full: Image.asset(
                                        'assets/images/like_evaluate.png'),
                                    empty: Image.asset(
                                        'assets/images/unlike_evaluate.png'),
                                  ),
                                  itemPadding:
                                      EdgeInsets.symmetric(horizontal: 4.0),
                                  onRatingUpdate: (rating) {
                                    setState(() {
                                      this.likeCount[1] = rating;
                                    });
                                  },
                                ),
                              ],
                            )
                          ],
                        ),
                      ],
                    )),

                //评价文本框
                Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(6)),
                    margin:
                        EdgeInsets.only(top: setWidth(12), bottom: setWidth(7)),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Row(
                            children: [
                              Container(
                                child: Image.asset(
                                  "assets/images/pen.png",
                                  fit: BoxFit.cover,
                                ),
                                width: setWidth(12),
                                height: setWidth(12),
                              ),
                              SizedBox(
                                width: setWidth(8),
                              ),
                              Container(
                                child: Text(
                                  "写下你的评价吧",
                                  style: TextStyle(
                                      color: Color(0xff989898),
                                      fontSize: setSp(16)),
                                ),
                              )
                            ],
                          ),
                          margin: EdgeInsets.only(
                              top: setWidth(17), left: setWidth(25)),
                        ),
                        Container(
                          constraints: BoxConstraints(
                            maxHeight: 500.0,
                            minHeight: 300.0,
                          ),
                          child: TextField(
                            style: TextStyle(fontSize: setSp(16)),
                            maxLines: null,
                            cursorColor: YTStyle.mainColor,
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.only(
                                  left: setWidth(15),
                                  top: setWidth(10),
                                  bottom: setWidth(10)),
                              isDense: true,
                              border: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              enabledBorder: InputBorder.none,
                            ),
                          ),
                        )
                      ],
                    )),
                GestureDetector(
                  onTap: () {
                    urlPush(context, "yuntu://evaluation_success");
                  },
                  child: Container(
                    margin: EdgeInsets.only(
                        left: setWidth(15),
                        right: setWidth(15),
                        top: setWidth(48),
                        bottom: setWidth(50)),
                    padding: EdgeInsets.only(
                        top: setWidth(10), bottom: setWidth(10)),
                    decoration: BoxDecoration(
                      color: YTStyle.mainColor,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "提交",
                          style: TextStyle(
                              color: Colors.white, fontSize: setSp(18)),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

//确认要退出评价？
  void _showDialog() {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(
              '确认要退出评价？',
              style: TextStyle(
                  color: Color(0xff323232),
                  fontSize: setSp(20),
                  fontWeight: FontWeight.w600),
            ),
            content: Text(
              '退出后编辑过得内容将不保存哦',
              style: TextStyle(
                color: Color(0xff323232),
                fontSize: setSp(16),
              ),
            ),
            actions: <Widget>[
              FlatButton(
                textColor: YTStyle.textColor,
                onPressed: () => Navigator.pop(context),
                child: Text('取消'),
              ),
              FlatButton(
                onPressed: () {
                  Navigator.pop(context);
                  Navigator.pop(context);
                },
                textColor: YTStyle.textColor,
                child: Text('确认'),
              ),
            ],
          );
        });
  }
}
